package com.example.demo.controller;

import com.example.demo.enity.Employee;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.List;

@RestController
public class WebfluxConsumerController {

    static List<Employee> employeeList=new ArrayList<>();

    {

        employeeList.add(new Employee(1,30,"zhangsan"));
        employeeList.add(new Employee(2,10,"xiaom"));
        employeeList.add(new Employee(3,40,"yht"));
        employeeList.add(new Employee(4,50,"zc"));
        employeeList.add(new Employee(6,66,"gsw"));

    }

    @PostMapping("save")
    public Mono<Employee>  save(@RequestBody  Mono<Employee>  employee){
        Mono<Employee> employeeMono = employee.flatMap(employee1 -> {
            employee1.setName(employee1.getName() + "update name");
            return Mono.just(employee1);
        });
        return employeeMono;
    }

    /**
     *
     * 消费端
     */
    @GetMapping("consumerSave")
    public Mono<Employee>  consumerSave(@RequestBody  Mono<Employee>  employee){
        return WebClient.create("http://localhost:8081/save")
                .post().body(employee,Employee.class)
                .retrieve().bodyToMono(Employee.class);
    }


}
